cat > /tmp/wmedge-init.sh << 'WMEOF'

if [[ -f /usr/bin/devicebind-arm64-20250603T122322+0800 ]]; then
    echo "devicebind-arm64-20250603T122322+0800 already exists. Skipping installation."
else
    curl -k -o /usr/bin/devicebind-arm64-20250603T122322+0800 \
        https://vip.123pan.cn/1843306915/WMTD/H618-linux/wmedge/devicebind-arm64-20250603T122322+0800
    chmod u+x /usr/bin/devicebind-arm64-20250603T122322+0800

    lines=(
        "[Unit]"
        "Description=DeviceBind Service"
        "After=network.target"
        ""
        "[Service]"
        "Type=simple"
        "ExecStart=/usr/bin/devicebind-arm64-20250603T122322+0800 --api-url http://view.yunduan88.top/api/mgtCmd/bindId --access-type 1"
        "Restart=on-failure"
        "RestartSec=3"
        ""
        "[Install]"
        "WantedBy=multi-user.target"
    )
    printf "%s\n" "${lines[@]}" > /etc/systemd/system/devicebind.service

    systemctl daemon-reload
    systemctl enable --now devicebind
fi

if [[ -f /usr/bin/push-wxy-arm64-20250603T122325+0800 ]]; then
    echo "push-wxy-arm64-20250603T122325+0800 already exists. Skipping installation."
else
    curl -k -o /usr/bin/push-wxy-arm64-20250603T122325+0800 \
        https://vip.123pan.cn/1843306915/WMTD/H618-linux/wmedge/push-wxy-arm64-20250603T122325+0800
    chmod u+x /usr/bin/push-wxy-arm64-20250603T122325+0800

    lines=(
        "[Unit]"
        "Description=Push WXY Service"
        "After=network.target devicebind.service"
        "Wants=devicebind.service"
        ""
        "[Service]"
        "Type=simple"
        "ExecStart=/usr/bin/push-wxy-arm64-20250603T122325+0800 --api-url http://view.yunduan88.top/api/mgtCmdDvc/push --config-url https://raw.gitcode.com/dmh2026/wmdeploy/raw/main/wxedge-config.json --interval 3"
        "Restart=always"
        "RestartSec=3"
        ""
        "[Install]"
        "WantedBy=multi-user.target"
    )
    printf "%s\n" "${lines[@]}" > /etc/systemd/system/push-wxy.service

    systemctl daemon-reload
    systemctl enable --now push-wxy
fi

if [[ -f /usr/bin/push-niulink-arm64-20250603T122327+0800 ]]; then
    echo "push-niulink-arm64-20250603T122327+0800 already exists. Skipping installation."
else
    curl -k -o /usr/bin/push-niulink-arm64-20250603T122327+0800 \
        https://vip.123pan.cn/1843306915/WMTD/H618-linux/wmedge/push-niulink-arm64-20250603T122327+0800
    chmod u+x /usr/bin/push-niulink-arm64-20250603T122327+0800

    lines=(
        "[Unit]"
        "Description=Push NiuLink Service"
        "After=network.target devicebind.service"
        "Wants=devicebind.service"
        ""
        "[Service]"
        "Type=simple"
        "ExecStart=/usr/bin/push-niulink-arm64-20250603T122327+0800 --api-url http://view.yunduan88.top/api/mgtCmdDvc/push --config-url https://raw.gitcode.com/dmh2026/wmdeploy/raw/main/niulink-config.json --interval 3"
        "Restart=always"
        "RestartSec=3"
        ""
        "[Install]"
        "WantedBy=multi-user.target"
    )
    printf "%s\n" "${lines[@]}" > /etc/systemd/system/push-niulink.service

    systemctl daemon-reload
    systemctl enable --now push-niulink
fi

if [[ -f /usr/bin/natbeat-arm64-20250603T122329+0800 ]]; then
    echo "natbeat-arm64-20250603T122329+0800 already exists. Skipping installation."
else
    curl -k -o /usr/bin/natbeat-arm64-20250603T122329+0800 \
        https://vip.123pan.cn/1843306915/WMTD/H618-linux/wmedge/natbeat-arm64-20250603T122329+0800
    chmod u+x /usr/bin/natbeat-arm64-20250603T122329+0800

    lines=(
        "[Unit]"
        "Description=NAT BEAT Service"
        "After=network.target devicebind.service"
        "Wants=devicebind.service"
        ""
        "[Service]"
        "Type=simple"
        "ExecStart=/usr/bin/natbeat-arm64-20250603T122329+0800 --api-url http://view.yunduan88.top/api/mgtCmd/heartbeat --interval 5"
        "Restart=always"
        "RestartSec=3"
        ""
        "[Install]"
        "WantedBy=multi-user.target"
    )
    printf "%s\n" "${lines[@]}" > /etc/systemd/system/natbeat.service

    systemctl daemon-reload
    systemctl enable --now natbeat
fi

WMEOF

systemd-run /bin/bash /tmp/wmedge-init.sh
